﻿﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Security.Claims;
using Microsoft.AspNetCore.Http.Authentication;
using Microsoft.AspNetCore.Authorization;
using Stock.Core;
using Stock.Core.Common;
using Stock.Model;
using Stock.Model.ViewModels;
using Stock.Core.Service;
using Newtonsoft.Json;
using System.Collections.Generic;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Web.Controllers
{
    public class StockController : BaseApiController
    {
        private readonly IStock1Service _s;
        private readonly IStockInfoService _stockinfo;
        private readonly ILoggerService _l;

        public StockController(IStock1Service s, IStockInfoService stockinfo, ILoggerService l)
        {
            _s = s;
            _stockinfo = stockinfo;
            _l = l;
        }

        private static NextTime nextTime { get; set; }

      
        [HttpPost()]
        [AllowAnonymous]
        public async Task<dynamic> InsertTick(K5ViewData data)
        {
            if(data.isnew == 1)
            {
				switch (data.type)
				{
					case 1:
						await _s.insert1(data);
						break;
					case 5:
						await _s.insert5(data);
						break;
					case 15:
						await _s.Insert15(data);
						break;
					case 30:
						await _s.Insert30(data);
						break;
					case 60:
						await _s.Insert60(data);
						break;
					case 240:
						await _s.Insert4h(data);
						break;
				}
            }
            else
            {
                var kdate = TimeHelper.GetTime(data.date.ToString());
                data.dtdate = kdate;
                NetManager.sendMsg(JsonConvert.SerializeObject(new { type = data.type, data = data, isnew = 0 }));
            }

            return 1;
        }


        public async Task<dynamic> Insert5(K5ViewData data)
        {

            var kdate =  TimeHelper.GetTime(data.date.ToString());

            kdate = new DateTime(kdate.Year, kdate.Month, kdate.Day, kdate.Hour, kdate.Minute, 0);

            data.dtdate = kdate;

            stock_5 s5 = new stock_5()
            {
                date = kdate,
                begin = data.begin,
                end = data.end,
                high = data.high,
                low = data.low
            };

            if(data.isnew== 1)
            {
				await _s.insert5(data);
			}
            else
            {
                NetManager.sendMsg(JsonConvert.SerializeObject(new { type = 0, data = data, isnew = 0 }));
            }


            return data.isnew;

        }

       

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns>The list.</returns>
        /// <param name="starttime">Starttime.</param>
        [HttpGet()]
        public async Task<dynamic> getList(DateTime? starttime)
        {
            return await _stockinfo.getList(starttime);
        }

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
		public async Task<dynamic> getLast()
		{
            return await _stockinfo.getLast();
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
		public async Task<dynamic> get1Last()
		{
            return await _s.get1Last();
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
        [AllowAnonymous]
		public async Task<dynamic> get1Today()
		{
            //// 算下一个开始
            //var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

            //         date = date.AddHours(-8);

            //var datelong = TimeHelper.ConvertDateTimeInt(
            //	date);
            //return await _s.get1Today(datelong);
            return await _s.get1Top();
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
		public async Task<dynamic> get5Last()
		{

			return await _s.get5Last();
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
        [AllowAnonymous]
		public async Task<dynamic> get5Today()
		{
            //// 算下一个开始
            //var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            //         date = date.AddDays(-2);

            //var datelong = TimeHelper.ConvertDateTimeInt(
            //             date);
            //return await _s.get5Today(datelong);
            return await _s.get5Top();
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
		public async Task<dynamic> get15Last()
		{
			return await _s.get15Last();
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
        [AllowAnonymous]
		public async Task<dynamic> get15Today()
		{
            //// 算下一个开始
            //var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            //         date = date.AddDays(-10);

            //var datelong = TimeHelper.ConvertDateTimeInt(
            //	date);
            //return await _s.get15Today(datelong);
            return await _s.get15Top();
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
        [AllowAnonymous]
		public async Task<dynamic> get30Today()
		{
			// 算下一个开始
			var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            date = date.AddDays(-15);

			var datelong = TimeHelper.ConvertDateTimeInt(
				date);
			return await _s.get30Today(datelong);
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
        [AllowAnonymous]
		public async Task<dynamic> get60Today()
		{
			// 算下一个开始
			var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            date = date.AddDays(-30);

			var datelong = TimeHelper.ConvertDateTimeInt(
				date);
			return await _s.get60Today(datelong);
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
        [AllowAnonymous]
		public async Task<dynamic> get4hToday()
		{
			// 算下一个开始
			var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            date = date.AddDays(-60);

			var datelong = TimeHelper.ConvertDateTimeInt(
				date);
			return await _s.get4hToday(datelong);
		}

		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
        [AllowAnonymous]
		public async Task<dynamic> get1dToday()
		{
			// 算下一个开始
			var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            date = date.AddDays(200);

			var datelong = TimeHelper.ConvertDateTimeInt(
				date);
			return await _s.get1dToday(datelong);
		}


		/// <summary>
		/// 获取数据
		/// </summary>
		/// <returns>The list.</returns>
		/// <param name="starttime">Starttime.</param>
		[HttpGet()]
        public async Task<dynamic> getListByLongTime(long starttime, long? endtime = null)
		{
            return await _stockinfo.getList(starttime, endtime);
		}

        [HttpGet()]
        public async Task<dynamic> getStock1List(DateTime? starttime)
        {
            return await _s.getList(starttime);
        }

        [HttpGet()]
        public async Task<dynamic> todayHigh()
        {
            double value = await _s.todayHigh();
            return new BaseResult() { Data = value, IsSuccess = true };
        }

        [HttpGet()]
		public async Task<dynamic> todayLow()
		{
             double value = await _s.todayLow();
            return new BaseResult() { Data = value, IsSuccess = true };
		}

		[HttpGet()]
		public async Task<dynamic> getfirst(long starttime)
		{
            var value = await _s.getfirst(starttime);
            return value;
		}

        [AllowAnonymous]
        [HttpGet()]
        public async Task<dynamic> Get51000()
        {
            var value = await _s.get51000();
            return new BaseResult(){Data = value};
        }

		[AllowAnonymous]
        [HttpPost()]
		public async Task<dynamic> Inserts5(List<stock5_m> list)
		{
            var value = await _s.inserts5(list);
			return value;
		}

		[AllowAnonymous]
        [HttpGet()]
		public async Task<dynamic> Get151000()
		{
			var value = await _s.get151000();
			return new BaseResult() { Data = value };
		}

		[AllowAnonymous]
		[HttpPost()]
		public async Task<dynamic> Inserts15(List<stock15_m> list)
		{
			var value = await _s.inserts15(list);
			return value;
		}

		[AllowAnonymous]
        [HttpGet()]
		public async Task<dynamic> Get11000()
		{
			var value = await _s.get11000();
			return new BaseResult() { Data = value };
		}

		[AllowAnonymous]
		[HttpPost()]
		public async Task<dynamic> Inserts1(List<stock1_m> list)
		{
			var value = await _s.inserts1(list);
			return value;
		}

		[AllowAnonymous]
        [HttpGet()]
		public async Task<dynamic> Get301000()
		{
			var value = await _s.get301000();
			return new BaseResult() { Data = value };
		}

		[AllowAnonymous]
		[HttpPost()]
		public async Task<dynamic> Inserts30(List<stock30_m> list)
		{
			var value = await _s.inserts30(list);
			return value;
		}


		[AllowAnonymous]
        [HttpGet()]
		public async Task<dynamic> Get601000()
		{
			var value = await _s.get601000();
			return new BaseResult() { Data = value };
		}

		[AllowAnonymous]
		[HttpPost()]
		public async Task<dynamic> Inserts60(List<stock60_m> list)
		{
			var value = await _s.inserts60(list);
			return value;
		}




   //     [HttpGet()]
   //     public async Task<dynamic> Testcount5()
   //     {
			//DateTime time = DateTime.Now;
			//DateTime today = new DateTime(time.Year, time.Month, time.Day);

			//long today_start = TimeHelper.ConvertDateTimeInt(today);
			//long today_end = TimeHelper.ConvertDateTimeInt(today.AddDays(1).AddMinutes(-1));

        //    try
        //    {
        //        await _s.CountOrder(today_start, today_end);
        //    }
        //    catch (Exception e)
        //    {
        //        return new BaseResult() { Message = e.Message, IsSuccess = false};
        //    }

        //    return new BaseResult() { Message = "1" };

        //}
    }
}
